﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.HVAC.AuxiliaryCurve
{
	public class PlaneNumberPointsGet : PlaneGetPoint
	{
		public PlaneNumberPointsGet(ExternalCommandData cmdArgu, XYZ pointPickArgu, Element elementArgu, int xNumberArgu, int yNumberArgu, double xSideArgu, double ySideArgu, ref Dictionary<XYZ, XYZ> dicxArgu, ref Dictionary<XYZ, XYZ> dicYArgu) : base(cmdArgu, pointPickArgu, elementArgu, xSideArgu, ySideArgu, ref dicxArgu, ref dicYArgu)
		{
			this.xNumber = xNumberArgu;
			this.yNumber = yNumberArgu;
		}

		public override bool GetPoints()
		{
			Face faceofPoint = BoostFunction.GetFaceofPoint(this.m_cmd, this.pointPick, this.element);
			if (faceofPoint == null)
			{
				return false;
			}
			BoundingBoxUV boundingBox = faceofPoint.GetBoundingBox();
			double num = boundingBox.Min.DistanceTo(boundingBox.Max);
			if (num <= this.xSide || num < this.ySide || num <= this.m_cmd.Application.Application.ShortCurveTolerance)
			{
				return false;
			}
			Dictionary<UV, UV> dicArgu = new Dictionary<UV, UV>();
			Dictionary<UV, UV> dicArgu2 = new Dictionary<UV, UV>();
			if (!this.GetUVList(ref dicArgu, ref dicArgu2, boundingBox.Min, boundingBox.Max))
			{
				return false;
			}
			BoostFunction.ConvertUVToXYZ(dicArgu, faceofPoint, ref this.dicX);
			BoostFunction.ConvertUVToXYZ(dicArgu2, faceofPoint, ref this.dicY);
			return true;
		}

		private bool GetUVList(ref Dictionary<UV, UV> dicXArgu, ref Dictionary<UV, UV> dicYArgu, UV min, UV max)
		{
			if (this.xNumber < 1 && this.yNumber < 1)
			{
				return false;
			}
			double num = min.V + this.xSide;
			double num2 = max.V - this.xSide;
			double num3;
			if (this.xNumber == 1)
			{
				num3 = Math.Abs(num2 - num);
			}
			else
			{
				num3 = Math.Abs(num2 - num) / (double)(this.xNumber - 1);
			}
			double num4 = min.U + this.ySide;
			double num5 = max.U - this.ySide;
			double num6;
			if (this.yNumber == 1)
			{
				num6 = Math.Abs(num5 - num4);
			}
			else
			{
				num6 = Math.Abs(num5 - num4) / (double)(this.yNumber - 1);
			}
			double num7 = min.U + this.ySide;
			double num8 = max.V - this.xSide;
			if (num6 > 0.0)
			{
				for (int i = 0; i < this.yNumber; i++)
				{
					UV key = new UV(num7, min.V + this.xSide);
					UV value = new UV(num7, num8);
					dicYArgu.Add(key, value);
					num7 += num6;
				}
			}
			if (num6 > 0.0)
			{
				for (int j = 0; j < this.xNumber; j++)
				{
					UV key2 = new UV(min.U + this.ySide, num8);
					UV value2 = new UV(max.U - this.ySide, num8);
					dicXArgu.Add(key2, value2);
					num8 -= num3;
				}
			}
			return true;
		}

		protected int xNumber;

		protected int yNumber;
	}
}
